package com.frank.oj.service.entity.contest.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frank.model.entity.contest.ContestProblem;
import com.frank.model.entity.contest.ContestRecord;
import com.frank.oj.mapper.ContestProblemMapper;
import com.frank.oj.model.vo.ContestProblemVO;
import com.frank.oj.model.vo.ProblemFullScreenListVO;
import com.frank.oj.service.entity.contest.ContestProblemEntityService;
import com.frank.oj.service.entity.contest.ContestRecordEntityService;
import com.frank.oj.service.entity.contest.ContestRegisterEntityService;
import com.frank.oj.service.entity.user.UserInfoEntityService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/7
 */
@Service
public class ContestProblemEntityServiceImpl extends ServiceImpl<ContestProblemMapper, ContestProblem> implements ContestProblemEntityService {
    @Resource
    private ContestRecordEntityService contestRecordEntityService;
    @Resource
    private UserInfoEntityService userInfoEntityService;
    @Resource
    private ContestProblemMapper contestProblemMapper;

    @Async
    @Override
    public void syncContestRecord(Long pid, Long cid, String displayId) {

        LambdaUpdateWrapper<ContestRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ContestRecord::getPid,pid)
                .eq(ContestRecord::getCid,cid)
                .set(ContestRecord::getDisplayId,displayId); //比赛列表展示id

        contestRecordEntityService.update(updateWrapper);
    }

    @Override
    public List<ContestProblemVO> getContestProblemList(Long cid, Date startTime,
                                                        Date endTime, Date sealTime,
                                                        boolean isAdmin, String contestAuthorUid,
                                                        List<String> groupRootUidList, Boolean isContainsContestEndJudge) {

        List<String> superAdminUidList = userInfoEntityService.getSuperAdminUidList();
        superAdminUidList.add(contestAuthorUid);

        if (!CollectionUtils.isEmpty(groupRootUidList)){
            superAdminUidList.addAll(groupRootUidList);
        }

        return contestProblemMapper.getContestProblemList(cid, startTime, endTime, sealTime, isAdmin, superAdminUidList, !isContainsContestEndJudge);
    }

    @Override
    public List<ProblemFullScreenListVO> getContestFullScreenProblemList(Long cid) {
        return contestProblemMapper.getContestFullScreenProblemList(cid);
    }
}
